package activity;

import com.qzhy.female.R;

import Constant.Constance;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class AddPhoneActivity extends ActionBarActivity implements OnClickListener {

	private EditText mEtPhone;
	private EditText mEtYzm;
	private TextView mTvSend;
	private boolean isStop = false;
	private int count = 60;

	//testdffddddddw
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_phone);
		InitUI();
	}

	private void InitUI() {
		mEtPhone = (EditText) findViewById(R.id.et_phone);
		mEtYzm = (EditText) findViewById(R.id.et_yzm);
		mTvSend = (TextView) findViewById(R.id.tv_send);
		mTvSend.setOnClickListener(this);
		
		mTvBack = (TextView) findViewById(R.id.tv_back);
		mTvBack.setOnClickListener(this);
		
		mRtlComfirm = (RelativeLayout) findViewById(R.id.rtl_comfirm);
		mRtlComfirm.setOnClickListener(this);
		
		mEtPassword = (EditText) findViewById(R.id.et_password);
		mEtPassword2 = (EditText) findViewById(R.id.et_password2);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_send:
			if(!TextUtils.isEmpty(mEtPhone.getText().toString().trim())){
//				SMSSDK.getVerificationCode("86",mEtPhone.getText().toString().trim());
				myBinder.startRegis();
				setStart();
			}else {
				Toast.makeText(this, "电话不能为空", 1).show();
			}
			break;
		case R.id.tv_back:
			finish();
			break;
		case R.id.rtl_comfirm:
			Comfirm();
			break;
		default:
			break;
		}
	}
	private void Comfirm() {
		String password1 = mEtPassword.getText().toString().trim();
		String password2 = mEtPassword2.getText().toString().trim();
		if(!password1.equals(password2)){
			Toast.makeText(getApplicationContext(), "提示:两次输入的密码不一致，请重新输入", 1).show();
		}else {
			String phone = mEtPhone.getText().toString().trim();
			Intent intent = new Intent();	
			intent.putExtra("Phone", phone);
			intent.putExtra("Password", password1);
			setResult(Constance.INTENT.COMFIRM_PHONE, intent);
			finish();
		}
	}

	/**
	 * 初始化倒计时
	 */
	private void initTime() {
		count = myBinder.getCountRegis();
		if (count > 0) {
			setStart();
		}
	}
	private void setStart() {
		isStop = false;
		mTvSend.setText(myBinder.getCountRegis() + "s");
		mTvSend.setClickable(false);
		mTvSend.setBackgroundResource(R.color.gray);
		mTvSend.postDelayed(new Runnable() {

			@Override
			public void run() {
				if (isStop) {
					return;
				}
				count = myBinder.getCountRegis();
				mTvSend.setText(count + "s");
				if (count == 0) {
					setStop();
					return;
				}
				mTvSend.postDelayed(this, 1000);
			}
		}, 1000);
	}
	private void setStop() {
		isStop = true;
		mTvSend.setText("获取验证码");
		mTvSend.setClickable(true);
		mTvSend.setBackgroundResource(R.color.red);
	}
	@Override
	protected void onStart() {
		Intent service = new Intent(this, service.TimeServer.class);
		startService(service);
		bindService(service, conn, Context.BIND_AUTO_CREATE);
		super.onStart();
	}
	private service.TimeServer.MyBinder myBinder;
	private ServiceConnection conn = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			Toast.makeText(getApplicationContext(), "获取服务失败", 1).show();
		}
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Toast.makeText(getApplicationContext(), "获取服务成功", 1).show();
			myBinder = (service.TimeServer.MyBinder) service;
			initTime();
		}
	};
	private TextView mTvBack;
	private RelativeLayout mRtlComfirm;
	private EditText mEtPassword;
	private EditText mEtPassword2;
}
